package com.xinqing.test.algorithm.k8s.forecast;

import com.alibaba.fastjson.JSON;
import com.feidee.fdcommon.authorization.AuthorizationVerify;
import com.feidee.fdcommon.authorization.model.AuthInfoModel;
import com.feidee.fdcommon.configuration.CustomConfiguration;
import com.feidee.fdcommon.model.StringResponse;
import com.feidee.fdcommon.util.HttpUtil;
import com.xinqing.test.modle.algorithm.ForecastParam;
import org.apache.http.Consts;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author:CHQ
 * @Date:2020/9/29 17:03
 * @Description
 */
public class STead71aa6bbdd8b08 {
    public static void main(String args[]) throws Exception {
        Date d = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = sdf.format(d);

        ForecastParam forecastParam = new ForecastParam();
        forecastParam.setBusId("ad_wdl");

        HashMap<String, Object> featuresMap = new HashMap<>();


        featuresMap.put("c1", "ios");
        featuresMap.put("c2", "MyMoney");
        featuresMap.put("c5", "splash");
        featuresMap.put("c6", "appstore");
        featuresMap.put("u5", "0");
        featuresMap.put("u7", "0");
        featuresMap.put("u8", "0");
        featuresMap.put("u11", "0");
        featuresMap.put("u17", "0");
        featuresMap.put("u18", "0");
        featuresMap.put("u19", "0");
        featuresMap.put("u23", "0");
        featuresMap.put("u26", "0");
        featuresMap.put("u28", "0");
        featuresMap.put("u29", "0");
        featuresMap.put("u31", "0");
        featuresMap.put("u32", "0");
        featuresMap.put("u33", "0");
        featuresMap.put("u34", "0");
        featuresMap.put("u36", "0");
        featuresMap.put("u37", "0");
        featuresMap.put("u38", "0");
        featuresMap.put("u39", "0");
        featuresMap.put("u40", "0");
        featuresMap.put("u41", "0");
        featuresMap.put("u42", "0");
        featuresMap.put("u43", "0");
        featuresMap.put("u44", "0");
        featuresMap.put("u45", "0");
        featuresMap.put("us0", "0");
        featuresMap.put("us1", "0");
        featuresMap.put("us2", "0");
        featuresMap.put("us3", "0");
        featuresMap.put("us4", "0");
        featuresMap.put("n1", "0");
        featuresMap.put("n2", "0");
        featuresMap.put("n3", "0");
        featuresMap.put("n4", "0");
        featuresMap.put("n5", "0");
        featuresMap.put("n6", "0");
        featuresMap.put("n7", "0");
        featuresMap.put("n8", "0");
        featuresMap.put("n9", "0");
        featuresMap.put("n10", "0");
        featuresMap.put("t0", "0");
        featuresMap.put("t1", time);
        featuresMap.put("t2", "0");
        featuresMap.put("t3", "61.621857142857145");
        // featuresMap.put("t3", "62");
        featuresMap.put("t4", "0");
        featuresMap.put("t5", "0");
        featuresMap.put("t6", "0");
        featuresMap.put("t7", "0");
        featuresMap.put("t8", "0");
        featuresMap.put("t9", "102.57142857142857");
        //featuresMap.put("t9", "103");
        featuresMap.put("t10", "181000");
        featuresMap.put("t11", "107");
        featuresMap.put("bd1", "0");
        featuresMap.put("bd2", "0");
        featuresMap.put("bd3", "7");
        featuresMap.put("bd4", "0");
        featuresMap.put("bd5", "0");
        featuresMap.put("bd6", "0");
        featuresMap.put("bd7", "0");
        featuresMap.put("bd8", "0");
        featuresMap.put("mb", "chanpinyunying");
        featuresMap.put("label", "0");
        forecastParam.setFeatures(featuresMap);

        //多分类特征
        HashMap<String, Map<String, Object>> multiFeaturesMap = new HashMap<>();
        HashMap<String, Object> map = new HashMap<>();
        map.put("c3", "70");
        map.put("c4", "0");
        map.put("c7", "SP");
        map.put("c0", "17375");
        multiFeaturesMap.put("19548", map);

        HashMap<String, Object> map1 = new HashMap<>();
        map1.put("c3", "58");
        map1.put("c4", "0");
        map1.put("c7", "SP");
        map1.put("c0", "17395");
        multiFeaturesMap.put("17456", map1);

        forecastParam.setMultiFeatures(multiFeaturesMap);

        forecastParam.setTraceId("99999999999999");

        forecastParam.setModels(new String[]{"fc8344a4a0933841", "be3b75f2e9bf5285"});
       // forecastParam.setModels(new String[]{"9dcc214c8e34f044", "aaf1f353ef2bacd3"});
        forecastParam.setTop(0);

        String data = JSON.toJSONString(forecastParam);

        //分配的AccessKeyId
        String accessKeyId = "data_team";
        //分配的AccessSecret
        String accessSecret = "MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAJ6+UYiQtt2OYfyfBF8e12kjd6nnF0Zl1s23f7l+eSl07xQxoxACSXyO3aq/tF7g0IiQZev4SF7ZA/oAOIYlYo0CAwEAAQ==";

        //String url = "https://tg.feidee.net/model/16f6462c51510271";
        String url = "https://sml.suishoudata.com/model/ead71aa6bbdd8b08";

        //调用鉴权工具包计算鉴权信息,data不存则传空
        AuthInfoModel authInfo = AuthorizationVerify.getAuthInfo(url, data, accessKeyId, accessSecret);
        //获取GMT时间+
        String date = authInfo.getDate();
        //获取加密之后的data，若传入data为空，则密文也为空
        String ciphertext = authInfo.getCiphertext();
        //获取认证信息
        String authorization = authInfo.getAuthorization();

        //构造请求
        HttpPost method = null;
        try {
            method = new HttpPost(url);
            method.setHeader("Date", date);
            method.setHeader("Authorization", authorization);
            method.setHeader("AuthVersion", "2");
            if (ciphertext != null) {
                //加密之后data通过http body传输
                StringEntity stringEntity = new StringEntity(ciphertext, Consts.UTF_8);
                stringEntity.setContentEncoding("UTF-8");
                stringEntity.setContentType("application/json");
                method.setEntity(stringEntity);
            }
            CustomConfiguration.setString("http.connection.socket.timeout", "50000");
            StringResponse response = HttpUtil.post(method);
            System.out.println("rslt:" + response.getResponseBody());
        } finally {
            if (method != null) {
                method.releaseConnection();
            }
        }


    }
}
